package xyz.wangdanhua.controller;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import xyz.wangdanhua.annotation.GoAop;
import xyz.wangdanhua.api.OneService;
import xyz.wangdanhua.factorybean.AbstractFactoryBean;
import xyz.wangdanhua.factorybean.UserFactoryBean;
import xyz.wangdanhua.model.User;
import xyz.wangdanhua.service.RequestService;

@RestController
@RequestMapping(value = "abc")
public class OneController implements ApplicationContextAware {
  
  @Value("${a.b.c}")
  private String name;
  
  private ApplicationContext applicationContext;
  
//  @Autowired
//  private User profile;
  
  public OneController() {
    System.out.println("构造" + this.getClass().getName());
  }
  
//  @Autowired
  private RequestService service;
  @Autowired
  private OneService oneService;
  
  @RequestMapping(value= "test", method = RequestMethod.GET, produces = {"text/plain"}, params = {"a=1"})
//  @ResponseBody
  @GoAop
  public String test() throws Exception {
    AbstractFactoryBean<?> bean = applicationContext.getBean("&factoryBean", AbstractFactoryBean.class);
//    applicationContext.getBean("factoryBean");
    User object = bean.getObject();
    System.err.println(bean);
    System.err.println(object);
    
//    throw new RuntimeException("xxxx");
    System.out.println(oneService.sayHello());
    System.err.println("********+++++++++" + applicationContext.getBean(RequestService.class).getName());
    System.err.println(name);
//    profile.test();
    return "nihao";
  }
  
  @RequestMapping(value= "test2", method = RequestMethod.POST, produces = {"text/plain"})
  @ResponseBody
  @GoAop
  public String test2(User user) {
    
    return user.toString();
  }

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
     this.applicationContext = applicationContext;
    
  }
  
  public static void main(String[] args) {
    AbstractFactoryBean<? extends User> bean = new UserFactoryBean();
  }
  
}
